$(document).ready(function() {
                $('.error').hide();
                $('#txt_username').change(function(){
                    check();
                });
                $('#txt_password2').blur(function(){
                    if(!compare("txt_password", "txt_password2", "lbl_password2"));
                     $('#lbl_password2').fadeIn("slow").hide();
                });
                $('#txt_password').blur(function(){
                    
                    if(!check_null("txt_password", "lbl_password", 6, null, "lbl_password2"))
                         $('#lbl_password').fadeIn("slow").hide();
                });
                $('#txt_email').change(function(){
                    if(!check_email("txt_email","lbl_email2")){
                        if(!check_null("txt_email", "lbl_email", null, 50, "lbl_email2"))
                            $('#lbl_email').fadeIn("slow").hide();
                     $('#lbl_email2').fadeIn("slow").hide();
                     $('#lbl_email').fadeIn("slow").hide();
                    }
                });
            });
			
function check_null(obj, error_message_id, length_min, length_max, error_message_check_length_id) {
        var temp = $("#" + obj);
        if (temp.val() == "") {
            $("#" + error_message_id).fadeIn("slow").show();
            temp.focus();
            return false;
        } else {
            if (length_min != null) {
                if (temp.val().length < length_min) {
                    $("#" + error_message_check_length_id).fadeIn("slow").show();
                    temp.focus();
                    return false;
                }
            }

            if (length_max != null) {
                if (temp.val().length > length_max) {
                    $("#" + error_message_check_length_id).fadeIn("slow").show();
                    temp.focus();
                    return false;
                }
            }
            return true;
        }
    }
    function compare(obj, obj_to_compare, error_message_id) {
        var temp = $("#" + obj);
        var temp_to_compare = $("#" + obj_to_compare);
        if (temp.val() != temp_to_compare.val()) {
            $("#" + error_message_id).fadeIn("slow").show();
            temp_to_compare.focus();
            return false;
        }
        return true;
    }
    function echeck(d) {
        return /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(d);
    }
    //check email
    //obj: object form to compare
    //error_message_id: message id
    function check_email(obj, error_message_id) {
        var temp = $("#" + obj);
        if (!echeck(temp.val())) {
            $("#" + error_message_id).fadeIn("slow").show();
            temp.focus();           
            return false;
        }          
        return true;
    }
    function gotosite(){
        window.location="Home.aspx";
    }
    function check(){
        if(document.getElementById('txt_username').value==''){
            $('#lbl_username').fadeIn("slow").show();
                return false;
       }
       else{
       $.ajax({
            url: 'ajaxActions.aspx',
            type: 'GET',
            data:
            {
                username: $('#txt_username').val(),
                cmd: 'check'
            }, 
            timeout: 2000,
            error: function(){
                alert('co loi');
            },
            success: function(msg){
                $('#lbl_username').fadeIn("slow").show();
                document.getElementById('lbl_username').innerHTML = msg
            }
      }); // Ajax      
      }
    }
    $(function () {
        $('.error').hide();
        $('#lblMessage').hide();
        $("#bt_reg").click(function () {

            $('.error').hide();
            var $form = $("#frmRegister");
            var username = $('#txt_username').val();
            var password = $('#txt_password').val();
            var password2 = $('#txt_password2').val();
            var email = $('#txt_email').val();
            var fullname = $('#txt_fullname').val();
            var address = $('#txt_address').val();
            var phone = $('#txt_phone').val();
            var thongtintaikhoan = $('#thongtintaikhoan').val();
            var cmnd = $('#cmnd').val();
            //begin validate form

            if (!check_null("txt_password", "lbl_password", 6, null, "lbl_password2") ||
                !compare("txt_password", "txt_password2", "lbl_password2") ||
                !check_null("txt_email", "lbl_email", null, 50, "lbl_email2") ||
                !check_email("txt_email", "lbl_email2")) {
                return false;
            }
            else {
                password = MD5(password);
                var strData = "cmd=reg&username=" + username + "&password=" + password + "&email=" + email + "&fullname=" + fullname + "&address=" + address + "&phone=" + phone;
                alert(strData);
                $.ajax({
                    type: "POST",
                    url: "ajaxActions.aspx",
                    data: {
                        cmd: "Register",
                        username: username,
                        password: password,
                        email: email,
                        fullname: fullname,
                        addr: address,
                        phone: phone,
                        cmnd: cmnd,
                        thongtintaikhoan: thongtintaikhoan
                    },
                    success: function (msg) {
                        alert(msg);
                    }
                });
                return false;
            } // end else
        }); // end function click
    });  // end document